package net.lab1024.sa.admin.module.business.supercomputer.datasource.service;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.dao.DatasourceDao;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.entity.DatasourceEntity;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.form.DatasourceAddForm;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.form.DatasourceQueryForm;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.form.DatasourceUpdateForm;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.vo.DatasourceVO;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 111 Service
 *
 * @Author zxp
 * @Date 2024-09-27 09:37:05
 * @Copyright 11111
 */

@Service
@Slf4j
public class DatasourceService {

    @Resource
    private DatasourceDao datasourceDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<DatasourceVO> queryPage(DatasourceQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<DatasourceVO> list = datasourceDao.queryPage(page, queryForm);
        PageResult<DatasourceVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(DatasourceAddForm addForm) {
        DatasourceEntity datasourceEntity = SmartBeanUtil.copy(addForm, DatasourceEntity.class);
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        datasourceEntity.setCreateUser(requestUser.getUserName());
        datasourceDao.insert(datasourceEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(DatasourceUpdateForm updateForm) {
        DatasourceEntity datasourceEntity = SmartBeanUtil.copy(updateForm, DatasourceEntity.class);
        datasourceDao.updateById(datasourceEntity);
        System.out.println(datasourceEntity);
        return ResponseDTO.ok();
    }


    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer dataSourceId) {
        if (null == dataSourceId){
            return ResponseDTO.ok();
        }

        datasourceDao.deleteById(dataSourceId);
        return ResponseDTO.ok();
    }
}
